package cn.xxxy.fourm.Controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.xxxy.fourm.dao.BoardDao;
import cn.xxxy.fourm.dao.PostDao;
import cn.xxxy.fourm.dao.ReplyPostDao;
import cn.xxxy.fourm.dao.UserDao;
import cn.xxxy.fourm.entity.Board;
import cn.xxxy.fourm.entity.Post;
import cn.xxxy.fourm.entity.ReplyPost;
import cn.xxxy.fourm.entity.User;
import cn.xxxy.fourm.entity.UserRole;
import cn.xxxy.fourm.entity.UserState;
import cn.xxxy.fourm.model.BoardVo;
import cn.xxxy.fourm.model.Message;
import cn.xxxy.fourm.model.PostDto;
import cn.xxxy.fourm.model.ReplyPostDto;
import cn.xxxy.fourm.model.UserVo;
import cn.xxxy.fourm.service.PostService;
import cn.xxxy.fourm.service.ReplyPostService;
import cn.xxxy.fourm.service.UserService;

@Controller@RequestMapping("/front")
public class FrontController {
	@Autowired
	private UserDao userDao;
	@Autowired
	private BoardDao boardDao;
	@Autowired
	private PostDao postDao;
	@Autowired
	private ReplyPostDao replyPostDao;
	@Autowired
	private PostService postService;
	@Autowired
	private ReplyPostService replyPostService;
	@Autowired
	private UserService userService;
	@RequestMapping("/toIndex")
	public String toIndex(Model m,HttpSession s) {
		m.addAttribute("tops", postService.listTopPost());
		m.addAttribute("posts", postService.listPage(1, 20));
		User loginUser=(User) s.getAttribute("loginUser");
		if(loginUser!=null&&!loginUser.equals("")) {
			m.addAttribute("author", loginUser);
			return "/front/index2";
		}else {
			return "/front/index";
		}
	}
	@RequestMapping("/toLogin")
	public String toLogin() {
		return "/user/login";
	}
	@RequestMapping("/toRegister")
	public String toRegister() {
		return "/user/reg";
	}
	@RequestMapping("/userIndex")
	public String toUserIndex(Model m,HttpSession s) {
		User loginUser=(User) s.getAttribute("loginUser");
		List<PostDto> myPosts =postService.listMyPostsByUid(loginUser.getUid());
		List<ReplyPostDto> myReplyPost=replyPostService.listMyRePostsByUid(loginUser.getUid());
		m.addAttribute("myPosts", myPosts);
		m.addAttribute("mypostCount", myPosts.size());
		m.addAttribute("myReplyPost", myReplyPost);
		m.addAttribute("myReplyPostCount", myReplyPost.size());
		if(loginUser!=null&&!loginUser.equals("")) {
			m.addAttribute("author", loginUser);
			return "/user/index";
		}else {
			return "redirect:/front/toLogin";
		}
	}
	@RequestMapping("/userHome")
	public String toUserHome(Model m,HttpSession s) {
		User loginUser=(User) s.getAttribute("loginUser");
		User user=userDao.getOne(loginUser.getUid());
		m.addAttribute("loginUser", user);
		m.addAttribute("loginUser", loginUser);
		m.addAttribute("myPosts", postService.listMyPostsByUid(loginUser.getUid()));
		m.addAttribute("myRePosts", replyPostService.listMyRePostsByUid(loginUser.getUid()));
		if(loginUser!=null&&!loginUser.equals("")) {
			m.addAttribute("author", loginUser);
			return "/user/home";	
		}else {
			return "redirect:/front/toLogin";
		}	
	}
	@RequestMapping("/userMessage")
	public String toUserMessage(Model m,HttpSession s) {
		User loginUser=(User) s.getAttribute("loginUser");
		m.addAttribute("loginUser", loginUser);
		if(loginUser!=null&&!loginUser.equals("")) {
			List<ReplyPostDto> myReplyPost=replyPostService.listRePostsByUid(loginUser.getUid());
			m.addAttribute("author", loginUser);
			m.addAttribute("myReplyPosts", myReplyPost);
			return "/user/message";		
		}else {
			return "redirect:/front/toLogin";
		}		
	}
	@RequestMapping("/userSet")
	public String toUserSet(Model m,HttpSession s) {
		User loginUser=(User) s.getAttribute("loginUser");
		User user=userDao.getOne(loginUser.getUid());
		m.addAttribute("loginUser", user);
		if(loginUser!=null&&!loginUser.equals("")) {
			m.addAttribute("author", loginUser);
			return "/user/set";		
		}else {
			return "redirect:/front/toLogin";
		}	
	}
	@PostMapping("/editUserInfo")
	public String toEditUserInfo(String email,String phone,String address,String sign,String uid) {
		userService.updateEmailAndPhoneAndAddressAndSign(email,phone,address,sign,Integer.parseInt(uid));
		return "redirect:/front/userSet";		
	}
	@PostMapping("/editPass")
	public String toEditPass(String email,String phone,String address,String sign,String uid) {
		userService.updateEmailAndPhoneAndAddressAndSign(email,phone,address,sign,Integer.parseInt(uid));
		return "redirect:/front/userSet";		
	}
	/**
     * .前台注册
     * @param user
     * @return
     */
	@PostMapping("/save")
	public String regUser(User user,int rid,int sid,Model m) {
		Message message=new Message();
		UserState userState =new UserState();
		userState.setSid(sid);
		UserRole userRole=new UserRole();
		userRole.setRid(rid);
		user.setUserState(userState);
		user.setUserRole(userRole);
		User user1=userDao.saveAndFlush(user);
		if(user1!=null) {
			message.setMsg("恭喜您，添加成功");
			message.setSuccess(true);
		}else {
		message.setMsg("很遗憾，添加失败");
		message.setSuccess(false);
		}
		m.addAttribute("message", message);
		return "redirect:/front/toLogin";
	}
	@RequestMapping("/toPost")
	public String toPost(Model m,HttpSession s) {
		User loginUser=(User) s.getAttribute("loginUser");
		m.addAttribute("uid", loginUser.getUid());
		List<Board> lists=boardDao.findAllBoard();
		List<BoardVo> list1=new ArrayList<>();
		for (Board board : lists) {
			BoardVo boardVo=new BoardVo();
			boardVo.setBid(board.getBid());
			boardVo.setText(board.getBname());
			if(board.getBid()==1) {
				boardVo.setSelected(true);
			}else {
				boardVo.setSelected(false);
			}
			list1.add(boardVo);
		}
		m.addAttribute("boards", list1);
		return "/jie/add";
	}
	/**
	 * 发帖
     * @param user
     * @return
     */
	@PostMapping("/savePost") 
	public String save(Post post,int bid,int uid) {
		Board board=new Board();
		board.setBid(bid);
		User user=new User();
		user.setUid(uid);
		post.setUser(user);
		post.setBoard(board);
		post.setTop("否");
		post.setComplete("未結");
		post.setQuality("否");
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		post.setPosttime(sdf.format(new Date()));
		postDao.saveAndFlush(post);	
		return "redirect:/front/userIndex"; 
		}
	@RequestMapping("/postDetail")
	public String toPostDetail(int pid,Model m,HttpSession s) {
		User loginUser=(User) s.getAttribute("loginUser");
		m.addAttribute("loginUser", loginUser);
		PostDto postDto=postService.getPostDetail(pid);
		m.addAttribute("postDto", postDto);
		return "/jie/detail";		
	}
	@PostMapping("/replyPost")
	public String toreplyPost(Integer uid,Integer pid,ReplyPost replyPost) {
		if(uid==null||uid==0) {
			return "redirect:/front/toLogin";
		}else {
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		replyPost.setReplytime(sdf.format(new Date()));
		User user=new User();
		user.setUid(uid);
		replyPost.setUser(user);
		Post post=new Post();
		post.setPid(pid);
		replyPost.setPost(post);
		replyPost.setAdopt("否");
		replyPost.setRtitle("评论");
		replyPostDao.saveAndFlush(replyPost);
		return "redirect:/front/postDetail?pid="+pid+"";
		}
	}
	@RequestMapping(value="/moreDetail/{index}",method= RequestMethod.GET)
	public String toMoreDetail(Model m,@PathVariable("index") Integer index) {
		long count=postDao.count();
		if(index==1) {
			m.addAttribute("counts", count);
			m.addAttribute("posts", postService.listPage(1, 10));
		}else {
			m.addAttribute("counts", count);
			m.addAttribute("posts", postService.listPage(index, 10));
		}
		return "/jie/index";	
	}
	
	@RequestMapping("/unCompleteDetail")
	public String toUnCompleteDetail(Model m) {
		m.addAttribute("posts", postService.listUnCompletePost());
		return "/jie/index";	
	}
	@RequestMapping("/completeDetail")
	public String toCompleteDetail(Model m) {
		m.addAttribute("posts", postService.listCompletePost());
		return "/jie/index";	
	}
	@RequestMapping("/qualityDetail")
	public String toQualityDetail(Model m) {
		m.addAttribute("posts", postService.listQualityPost());
		return "/jie/index";	
	}
	@RequestMapping("/newDetail")
	public String toNewDetail(Model m) {
		m.addAttribute("posts", postService.listNewPost());
		return "/jie/index";	
	}
	@RequestMapping("/hotDetail")
	public String toHotDetail(Model m) {
		m.addAttribute("posts", postService.listHotPost());
		return "/jie/index";	
	}
	@RequestMapping("/keySearchDetail")
	public String tokeySearchDetail(Model m,String key) {
		m.addAttribute("posts", postService.listkeySearchPost(key));
		return "/jie/index";	
	}
	
}
